from django.db.models import F
from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView

from app01.models import *
from app01.sers import *


class UserView(APIView):
    def post(self,request):
        username=request.data.get('username')
        user=User.objects.filter(username=username)
        if user:
            return Response({'msg':'登录成功','status':200})
        return Response({'msg':'登录失败','status':400})

    def get(self,request):
        queryset=User.objects.all()
        ser=UserSer(queryset,many=True)
        return Response(ser.data)

    def put(self,request):
        id = int(request.data.get('id'))
        print(id)
        if id==1:
            User.objects.filter(user_level=id).update(score=F('score') + 10)
        # 通过id查询对应新闻， 修改点赞数增1
            return Response({'msg': '评论成功，以加分'})
        elif id==2:
            User.objects.filter(user_level=id).update(score=F('score') + 20)
            return Response({'msg': '评论成功，以加分'})
class JournalismView(APIView):
    def get(self,request):
        queryset=Journalism.objects.all()
        ser=JournalismSer(queryset,many=True)
        return Response(ser.data)
